package edu.gatech.ealf.ttsplaf;

import javax.swing.UIDefaults;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

import edu.gatech.ealf.AuxiliaryLookAndFeel;
import edu.gatech.ealf.AuxiliaryUIDefaults;

public class TextToSpeechLookAndFeel extends AuxiliaryLookAndFeel {

    public static final String KEVIN16 = "kevin16";
    public static final String KEVIN = "kevin";
    // Apparently doesn't work
    public static final String ALAN = "alan";
    
    private String voiceString = KEVIN16;
    
    
	public static Voice voice = null;
	
	private static TextToSpeechLookAndFeel instance = null;
	
	public static TextToSpeechLookAndFeel getInstance(){
    	if(instance == null){
    		instance = new TextToSpeechLookAndFeel();
    	}
    	return instance;
    }
	
	private TextToSpeechLookAndFeel(){

        uidefaults = new AuxiliaryUIDefaults();
        Object[] uiDefaults = new Object[] {
         "ButtonUI" , "edu.gatech.ealf.ttsplaf.TTSButtonUI",
         "MenuBarUI", "edu.gatech.ealf.ttsplaf.TTSMenuBarUI",
         "MenuItemUI", "edu.gatech.ealf.ttsplaf.TTSMenuItemUI",
         "SliderUI", "edu.gatech.ealf.ttsplaf.TTSSliderUI",
         "ComboBoxUI", "edu.gatech.ealf.ttsplaf.TTSComboBoxUI",
         "TextFieldUI", "edu.gatech.ealf.ttsplaf.TTSTextUI",
         "TextAreaUI", "edu.gatech.ealf.ttsplaf.TTSTextUI",
         "TextPaneUI", "edu.gatech.ealf.ttsplaf.TTSTextUI",
         "PasswordFieldUI", "edu.gatech.ealf.ttsplaf.TTSTextUI",
         "LabelUI", "edu.gatech.ealf.ttsplaf.TTSLabelUI",
        };
        uidefaults.putDefaults(uiDefaults);
        
		//uidefaults.put("ButtonUI", "edu.gatech.ealf.ttsplaf.TTSButtonUI");
		VoiceManager voiceManager = VoiceManager.getInstance();
		voice = voiceManager.getVoice(voiceString);
		if(voice == null){
			System.err.println("VOICE IS NULL!!!");
		}
		/*
		 * Allocates the resources for the voice.
		 */
		voice.allocate();
	}
	
	@Override
	public void finalize(){
		voice.deallocate();
	}
	
	@Override
	public String getDescription() {
		return "A Look and Feel that renders UI objects as speech";
	}

	@Override
	public String getID() {
		return "TextToSpeechLookAndFeel";
	}

	@Override
	public String getName() {
		return "TextToSpeechLookAndFeel";
	}

	@Override
	public boolean isNativeLookAndFeel() {
		return false;
	}

	@Override
	public boolean isSupportedLookAndFeel() {
		return true;
	}
	
	@Override
	public UIDefaults getDefaults() {
        return uidefaults;
    }

    public String getVoiceString() {
        return voiceString;
    }

    public void setVoiceString(String voiceString) {
        if(this.voiceString.equals(voiceString)){
            return;
        }
        this.voiceString = voiceString;
        if(voice != null){
            voice.deallocate();
        }
        VoiceManager voiceManager = VoiceManager.getInstance();
        voice = voiceManager.getVoice(voiceString);
        voice.allocate();
    }

}
